// console.show(true);
let test = false;
//获取设置
let configParas = global.WEB_PARAMS;

if (test) {
    configParas = {

    };
}

//同城
//1、独享
let 同独 = configParas.checkedD;
let 同独总价 = configParas.totalMoneyD;
let 同独人数 = configParas.peopleD;
let 同独单价 = configParas.perMoneyD;
let 同独距离 = configParas.distanceD;
//2、拼单
let 同拼 = configParas.checkedP;
let 同拼总价 = configParas.totalMoneyP;
let 同拼人数 = configParas.peopleP;
let 同拼单价 = configParas.perMoneyP;
let 同拼距离 = configParas.distanceP;
//3、送货
let 同货 = configParas.checkedH;
let 同货总价 = configParas.totalMoneyH;
let 同货重量 = configParas.weight;
let 同货单价 = configParas.perMoneyH;
let 同货距离 = configParas.distanceH;
//跨城
//1、独享
let 跨独 = configParas.checkedCD;
let 跨独总价 = configParas.totalMoneyCD;
let 跨独人数 = configParas.peopleCD;
let 跨独单价 = configParas.perMoneyCD;
let 跨独距离 = configParas.distanceCD;
//2、拼单
let 跨拼 = configParas.checkedCP;
let 跨拼总价 = configParas.totalMoneyCP;
let 跨拼人数 = configParas.peopleCP;
let 跨拼单价 = configParas.perMoneyCP;
let 跨拼距离 = configParas.distanceCP;
//3、送货
let 跨货 = configParas.checkedCH;
let 跨货总价 = configParas.totalMoneyCH;
let 跨货重量 = configParas.weightC;
let 跨货单价 = configParas.perMoneyCH;
let 跨货距离 = configParas.distanceCH;

let 模式 = configParas.type;



events.observeToast();
events.onToast(function (toast) {
    log("Toast内容: " + toast.getText() + " 包名: " + toast.getPackageName());
});


var errCount = 0;
while (true) {
    //检查当前是不是在指定的页面
    try {
        let nearFind = text("市内路线").id("tvNearPax");
        let acrossFind = text("城际路线").id("tvAcrossPax");
        let selectBg = id("tvSelectedBg").findOne(3000);
        if (nearFind.exists() && acrossFind.exists()) {
            if (selectBg.bounds().left > 200) {
                if (模式 === "同城") {
                    toastLog('当前为只接同城单,请切换到市内路线');
                    sleep(3000);
                }
                log("跨城路线");
                let findRv = id("rvMatchOrders").className("androidx.recyclerview.widget.RecyclerView");
                if (findRv.exists()) {
                    let rv = findRv.findOne(2000);
                    rv.children().forEach(relativeLayout => {

                        if (relativeLayout.childCount() > 1 && relativeLayout.child(1)) {
                            log("看过了");
                        } else {
                            let item = relativeLayout.child(0);
                            let type = item.findOne(id("tvDriverPooling").className("android.widget.TextView")).text();
                            let time = item.findOne(id("tvDate").className("android.widget.TextView")).text();
                            let startAddress = item.findOne(id("tvStartAddressCross").className("android.widget.TextView")).text();
                            let startDistance = item.findOne(id("tvStartDistanceCross").className("android.widget.TextView")).text();
                            let money = item.findOne(id("tvAmount").className("android.widget.TextView")).text();
                            let peopleNum = item.findOne(id("tvDriverPeopleCount").className("android.widget.TextView")).text();
                            let cf = checkAcFirst(type, time, startDistance, money, peopleNum);
                            if (cf) {
                                log("⭐类型：" + type + ",人数：" + peopleNum + ",时间：" + time + ",接人在：" + startAddress + ",接人距离：" + startDistance + ",费用：" + money);
                                //checkAcSecond(type);
                                log("点击");
                                relativeLayout.click();
                                let 返回 = id('ivOrderBack').findOne(5000);
                                if (返回 == null) {
                                    throw new Error('没有进入订单界面');
                                } else {
                                    if (id("tv_pool_recommend_none").findOnce()) {
                                        log("没有顺路的订单");
                                        返回.click();
                                    } else {
                                        let 价格 = id('llAmount').findOnce();
                                        let 推荐列表=id('rv_pool_recommend_list').findOnce();
                                        推荐列表.scrollForward();
                                    }
                                }
                                sleep(10000000);
                            } else {
                                log("📖类型：" + type + ",人数：" + peopleNum + ",时间：" + time + ",接人在：" + startAddress + ",接人距离：" + startDistance + ",费用：" + money);
                            }
                        }
                    });
                }
                sleep(8000);
            } else {
                if (模式 === "跨城") {
                    toastLog('当前为只接跨城单,请切换到城际路线');
                    sleep(3000);
                }
                log("市内路线");
                let findRv = id("rvMatchOrders").className("androidx.recyclerview.widget.RecyclerView");
                if (findRv.exists()) {
                    let rv = findRv.findOne(2000);
                    rv.children().forEach(relativeLayout => {
                        let item = relativeLayout.child(0);
                        let time = item.findOne(id("tvDate").className("android.widget.TextView")).text();
                        let startDistance = item.findOne(id("tvStartAddress").className("android.widget.TextView")).text();
                        let startAddress = item.findOne(id("tvStartDistance").className("android.widget.TextView")).text();
                        let endDistance = item.findOne(id("tvEndAddress").className("android.widget.TextView")).text();
                        let endAddress = item.findOne(id("tvRegionDescribe").className("android.widget.TextView")).text();
                        let peopleNum = item.findOne(id("tvDriverPeopleCount").className("android.widget.TextView")).text();
                        let type = item.findOne(id("tvDriverPooling").className("android.widget.TextView")).text();
                        let money = item.findOne(id("tvAmount").className("android.widget.TextView")).text();
                        log("类型：" + type + ",时间：" + time + ",人数：" + peopleNum + ",接人在：" + startAddress + ",接人距离：" + startDistance + ",费用：" + money + ",送人在：" + endDistance + "-" + endAddress);
                    });
                    log(rv.childCount());
                    sleep(8000);
                }
            }
            errCount = 0;

        } else if (id("ivAvatar").exists() && id("tvName").exists() && id("tvButton").exists()) {
            log("信息界面");
            errCount = 0;
            sleep(1000);
        } else if (text("查看计价规则").exists() && text("行程信息").exists() && text("行程费用").exists()) {
            log("详细界面");
            errCount = 0;
            sleep(1000);
        }
        else {
            log("未知界面");
            errCount++;
            sleep(3600);
        }
    } catch (error) {
        log("出错了");
        log(error);
        sleep(1000);
    }

}
/**
 * 跨城初步校验
 * @returns 是否符合
 */
function checkAcFirst(type, time, startDistance, money, peopleNum) {
    let dis = 跨拼距离;
    let 总价 = 跨拼总价;
    let 人数 = 跨拼人数;
    if (type === "拼座" || type === "舒适拼") {
        if (!跨拼) {
            return false;
        }
    } else if (type === "独享") {
        if (!跨独) {
            return false;
        }
        dis = 跨独距离;
        总价 = 跨独总价;
        人数 = 跨独人数;
    } else if (type === "送货") {
        if (!跨货) {
            return false;
        }
        总价 = 跨货总价;
        dis = 跨货距离;
    }
    if (extractFloat(startDistance) > dis) {//太远
        log("接人太远")
        return false;
    }
    if (parseFloat(money) < 总价) {//总价太低
        log("总价太低")
        return false;
    }
    if (peopleNum > 人数) {//人数过多
        log("人数过多")
        return false;
    }
    return true;
}

function checkAcSecond(type) {
    //当前暂无 id:tv_pool_recommend_none  className:android.widget.TextView (这种直接不接)
    if (id("tv_pool_recommend_none").className("android.widget.TextView").exists()) {
        log("没有顺路的其它订单");
        return false;
    }

    return true;
}

function extractFloat(str) {
    const regex = /[+-]?\d+(\.\d+)?/g;
    const matches = str.match(regex);
    if (matches === null) {
        return 0.0;
    }
    return parseFloat(matches[0]);
}

function clickRect(rect) {
    click(random(rect.left, rect.right), random(rect.top, rect.bottom))
}
/**
 * 刷新列表
 */
function refresh() {
    id("btnRefresh").findOne().click();
}

//distanceInfo